<template>
  <PanerForm :isShowBarDrawer="true" :caption="'编辑任务'">
    <template #body>
      <div class="d-row">
        <h5>基本</h5>
        <div class="d-form-group d-form-group-flex">
          <div class="d-form-label d-text-right">
            <label class="label-text">任务名称</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.name" class="d-form-input" type="text" />
          </div>
        </div>
        <div class="d-form-group d-form-group-flex">
          <div class="d-form-label d-text-right">
            <label class="label-text">任务类型</label>
          </div>
          <div class="d-form-el">
            <select v-model="state.type" class="d-form-select">
              <option v-for="item in taskTypes" :key="item.id" :value="item.id">{{ item.text }}</option>
            </select>
          </div>
        </div>
        <div class="d-form-group d-form-group-flex">
          <div class="d-form-label d-text-right">
            <label class="label-text">描述</label>
          </div>
          <div class="d-form-el">
            <textarea v-model="state.description" class="d-form-textarea"></textarea>
          </div>
        </div>
      </div>
      <div class="d-row">
        <h5>触发器</h5>
        <Trigger v-model="trigger" />
      </div>
      <div class="d-row">
        <h5>操作</h5>
        <Operation v-model="operation" />
      </div>
      <div class="d-row">
        <div class="d-form-group d-form-group-flex">
          <div class="d-form-label d-text-right">
            <label class="label-text">状态</label>
          </div>
          <div class="d-form-el">
            <select v-model="state.status" class="d-form-select">
              <option v-for="item in statusList" :key="item.id" :value="item.id">{{ item.text }}</option>
            </select>
          </div>
        </div>
        <FormCalendar
          v-if="isEdit"
          v-model="state.updateTime"
          :isEditAble="false"
          :readonly="true"
          format="yyyy-MM-dd HH:mm:ss"
          label="修改时间"
          labelWidth="90px"
          class="d-form-group-flex d-col-xs-6"
        ></FormCalendar>
        <div v-if="false && isEdit" class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">修改人</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.updateUser" class="d-form-input" type="text" readonly />
          </div>
        </div>
        <FormCalendar
          v-if="isEdit"
          v-model="state.createTime"
          :isEditAble="false"
          :readonly="true"
          format="yyyy-MM-dd HH:mm:ss"
          label="创建时间"
          labelWidth="90px"
          class="d-form-group-flex d-col-xs-6"
        ></FormCalendar>
        <div v-if="false && isEdit" class="d-form-group d-form-group-flex d-col-xs-6">
          <div class="d-form-label d-text-right">
            <label class="label-text">创建人</label>
          </div>
          <div class="d-form-el">
            <input v-model="state.createUser" class="d-form-input" type="text" readonly />
          </div>
        </div>
      </div>
    </template>
    <template #bar-drawer>
      <button @click="handleSubmit" class="d-btn d-btn-primary" type="button">提交</button>
      <button v-if="!isEdit" @click="handleReset" class="d-btn d-btn-default" type="reset">重置</button>
      <a v-d-goback class="d-btn d-btn-default">返回</a>
    </template>
  </PanerForm>
</template>

<script>
import { PanerForm } from '@daelui/vdog/dist/pages.js'
import { Dialoger, FormCalendar } from '@daelui/vdog/dist/components.js'
import { zh as locale } from '@daelui/dogjs/dist/locale.js'

export default {
  components: {
    PanerForm,
    FormCalendar
  },

  data () {
    return {
      isEdit: false, // 是否是编辑模式
      // 表单数据
      state: {
        id: '',
        name: '',
        description: '',
        type: '',
        trigger: '',
        operation: '',
        status: 1,
        order: '',
        updateTime: '',
        updateUser: '',
        createTime: '',
        createUser: ''
      },
      // 状态列表
      statusList: [
        {id: 1, text: '启用'},
        {id: 0, text: '禁用'}
      ],
      // 触发器
      trigger: {},
      // 操作
      operation: {},
      // 笔记类型
      taskTypes: []
    }
  },

  methods: {
    /**
     * @function 加载笔记
    */
    loadItem (id) {
      // taskSer.queryItem({ id }).then(result => {
      //   let data = result.data || {}
      //   this.state = data
      //   try {
      //     this.trigger = JSON.parse(data.trigger || '{}')
      //   } catch (e) {
      //     this.trigger = {}
      //   }
      //   try {
      //     this.operation = JSON.parse(data.operation || '{}')
      //   } catch (e) {
      //     this.operation = {}
      //   }
      // })
    },

    /**
     * @function 提交
    */
    handleSubmit () {
      let defer = null
      try {
        this.state.trigger = JSON.stringify(this.trigger || {})
      } catch (e) {
        this.state.trigger = '{}'
      }
      try {
        this.state.operation = JSON.stringify(this.operation || {})
      } catch (e) {
        this.state.operation = '{}'
      }
      delete this.state.createTime
      // 编辑
      if (this.isEdit) {
        this.state.count = 0
        // defer = taskSer.editItem(this.state)
      } else { // 添加
        // defer = taskSer.addItem(this.state)
      }
      defer.then(result => {
        if (result.success) {
          Dialoger.success(locale.action.success)
          // 添加则回到列表页
          if (!this.isEdit) {
            this.$router.push(this.getMicroRoute('/task/list'))
          }
        } else {
          Dialoger.danger(locale.action.fail)
        }
      })
    },
    /**
     * @function 重置
    */
    handleReset () {

    }
  },

  beforeMount () {
    let params = this.$route.params
    // 新建
    if (!params.id) {
      this.isEdit = false
    } else { // 编辑
      this.isEdit = true
      this.loadItem(params.id)
    }
  },

  mounted () {
    // 查询笔记列表
    // taskTypeSer.queryList().then(res => {
    //   let list = res.data
    //   list = Array.isArray(list) ? list : []
    //   this.taskTypes = list
    // })
  }
}
</script>

<style scoped>
.d-form-label {
  width: 90px;
}
.d-form-textarea {
  min-height: 120px;
}
</style>
